package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Academy;
import model.CRN;
import model.Subject;
import model.SubjectContent;
import model.beans.CRNBean;
import model.beans.ObservationBean;
import model.beans.UsuarioBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;

import business.misc.Util;

import controler.Validate;


public class SubjectContentAction extends MappingDispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
				
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;		
		
		String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));    	
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	request.setAttribute("lst_unitContent", SubjectContent.list(str_subjectCode));
    	return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward editObservations(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
				
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		DynaActionForm forma = (DynaActionForm)form;
		
		HttpSession session = request.getSession();
		UsuarioBean user_log_t =(UsuarioBean)session.getAttribute("ses_usuario");
		
		String str_forward;
		if( ((Integer)forma.get("int_idObservation")).intValue() == 0 ){
			if( SubjectContent.save(forma.getMap()) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}else{
			if( SubjectContent.edit(forma.getMap()) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}
		
		if(str_forward.equals(Util.SUCCESS)){			
			request.setAttribute("req_crns", CRN.list(user_log_t.getId(), CRN.STUDENT));
		}else{
			
		}
    	return mapping.findForward(str_forward); 
	}

	public ActionForward listObservations(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
				
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
	    if(action_retVal!=null)return action_retVal;
		DynaActionForm forma = (DynaActionForm)form;
		

    	String str_crncode = request.getParameter("crn_code");
    	String str_ciclo = request.getParameter("crn_ciclo");
    	
		HttpSession session = request.getSession();
		UsuarioBean user_log = (UsuarioBean) session.getAttribute("ses_usuario");
		CRNBean crn_bean = CRN.instance(str_crncode, str_ciclo);
    	String str_idUser = user_log.getId();
    	
    	request.setAttribute("crn_bean", crn_bean);
    	
    	ObservationBean bn_obs = SubjectContent.obsInstance(crn_bean.getMateria_tk(), str_idUser, crn_bean.getMaestro_tk());
    	
    	forma.set("int_idObservation",new Integer(bn_obs.getInt_observationId()));
    	forma.set("str_descripcion", bn_obs.getStr_descripcion());
    	forma.set("str_idMateria", crn_bean.getMateria_tk());
    	forma.set("str_idAlumno", str_idUser);
    	forma.set("str_idMaestro", crn_bean.getMaestro_tk());
    	
    	return mapping.findForward(Util.DEFAULT); 
	}

}
